package cn.edu.zut.qixiang.mapper;

import cn.edu.zut.qixiang.dto.DistrictDailyWind;
import cn.edu.zut.qixiang.dto.HourlyChartDTO;
import cn.edu.zut.qixiang.dto.PrecipitationDTO;
import cn.edu.zut.qixiang.entity.WeatherHourly;
import org.apache.ibatis.annotations.*;

import java.util.List;



/**
 * @author 86178
 */
@Mapper
public interface WeatherHourlyMapper {

    List<PrecipitationDTO> selectSummerPrecipitation();

    List<DistrictDailyWind> getDistrictDailyAvgWindSpeed();

    List<HourlyChartDTO> selectHourlyData(
            @Param("district") String district,
            @Param("date") String date);

    @Select("SELECT * FROM weather_hourly WHERE hourly_id = #{id}")
    WeatherHourly findById(Integer id);

    @Select("SELECT * FROM weather_hourly WHERE district_id = #{districtId} " +
            "ORDER BY hour_time DESC LIMIT 24")
    List<WeatherHourly> findRecentByDistrict(Integer districtId);

    @Insert("INSERT INTO weather_hourly(district_id, daily_id, hour_time, temperature, " +
            "wind_direction, wind_speed, precipitation, pressure, cloud_cover, visibility) " +
            "VALUES(#{districtId}, #{dailyId}, #{hourTime}, #{temperature}, #{windDirection}, " +
            "#{windSpeed}, #{precipitation}, #{pressure}, #{cloudCover}, #{visibility})")
    @Options(useGeneratedKeys = true, keyProperty = "hourlyId")
    int insert(WeatherHourly weatherHourly);

    @Select("SELECT AVG(temperature) as avg_temperature, " +
            "MAX(temperature) as max_temperature, " +
            "MIN(temperature) as min_temperature, " +
            "SUM(precipitation) as total_precipitation, " +
            "AVG(wind_speed) as avg_wind_speed " +
            "FROM weather_hourly " +
            "WHERE district_id = #{districtId} " +
            "AND hour_time >= DATE_SUB(NOW(), INTERVAL 24 HOUR)")
    List<Object> get24HourStats(Integer districtId);

    @Select("SELECT COUNT(*) FROM weather_hourly")
    Integer getTotalCount();
}
